﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using LuxDiner.Model.Orders;
using LuxDiner.Model.Products;
namespace LuxDiner.Command.Orders
{
    public class RepertoryCommand
    {
        /// <summary>
        /// 总库存量
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static RepertoryCollection GetRepertories(RepertoryQuery sea)
        {
            var result = new RepertoryCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<view_repertory_tops> q = db.DataContext.view_repertory_tops;
                if (sea != null)
                {
                    if (sea.ProductID > 0)
                        q = q.Where(p => p.ProductID == sea.ProductID);
                    if (sea.HideZero)
                        q = q.Where(p => p.Balance != 0);
                }

                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderBy(p => p.ProductID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<RepertoryModel, view_repertory_tops, RepertoryCollection>(lst);
                    result.TotalRecordsCount = n;

                    if (sea != null)
                    {
                        foreach (var rep in result)
                        {
                            if (sea.GetProduct)
                            {
                                var product = db.DataContext.products.FirstOrDefault(p => p.ID == rep.ProductID);
                                rep.Product = Convertor.ConvertTo<ProductModel, product>(product);
                            }
                        }
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 分仓统计库存量
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static RepertoryCollection GetRepertoriesByHouse(RepertoryQuery sea)
        {
            var result = new RepertoryCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<view_repertory_house> q = db.DataContext.view_repertory_house;
                if (sea != null)
                {
                    if (sea.ProductID > 0)
                        q = q.Where(p => p.ProductID == sea.ProductID);
                    if (sea.HouseID > 0)
                        q = q.Where(p => p.HouseID == sea.HouseID);
                    if (sea.HideZero)
                        q = q.Where(p => p.Balance != 0);
                }

                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderBy(p => p.ProductID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<RepertoryModel, view_repertory_house, RepertoryCollection>(lst);
                    result.TotalRecordsCount = n;

                    if (sea != null)
                    {
                        foreach (var rep in result)
                        {
                            if (sea.GetProduct)
                            {
                                var product = db.DataContext.products.FirstOrDefault(p => p.ID == rep.ProductID);
                                rep.Product = Convertor.ConvertTo<ProductModel, product>(product);
                            }
                            if (sea.GetHouse)
                            {
                                var house = db.DataContext.warehouses.FirstOrDefault(p => p.ID == rep.HouseID);
                                rep.Warehouse = Convertor.ConvertTo<WarehouseModel, warehouse>(house);
                            }
                        }
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 分批统计库存量
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static RepertoryCollection GetRepertoriesByRetention(RepertoryQuery sea)
        {
            var result = new RepertoryCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<view_repertory_retention> q = db.DataContext.view_repertory_retention;
                if (sea != null)
                {
                    if (sea.ProductID > 0)
                        q = q.Where(p => p.ProductID == sea.ProductID);
                    if (sea.HouseID > 0)
                        q = q.Where(p => p.HouseID == sea.HouseID);
                    if (sea.HideZero)
                        q = q.Where(p => p.Balance != 0);
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderBy(p => p.ProductID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<RepertoryModel, view_repertory_retention, RepertoryCollection>(lst);
                    result.TotalRecordsCount = n;
                    
                    if (sea != null)
                    {
                        foreach (var rep in result)
                        {
                            if (sea.GetProduct)
                            {
                                var product = db.DataContext.products.FirstOrDefault(p => p.ID == rep.ProductID);
                                rep.Product = Convertor.ConvertTo<ProductModel, product>(product);
                            }
                            if (sea.GetHouse)
                            {
                                var house = db.DataContext.warehouses.FirstOrDefault(p => p.ID == rep.HouseID);
                                rep.Warehouse = Convertor.ConvertTo<WarehouseModel, warehouse>(house);
                            }
                        }
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 库存明细
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static RepertoryCollection GetDetails(RepertoryQuery sea)
        {
            var result = new RepertoryCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<repertory> q = db.DataContext.repertories;
                if (sea != null)
                {
                    if (sea.ProductID > 0)
                        q = q.Where(p => p.ProductID == sea.ProductID);
                    if (sea.HouseID > 0)
                        q = q.Where(p => p.HouseID == sea.HouseID);
                    if (!string.IsNullOrEmpty(sea.Retention))
                        q = q.Where(p => p.Retention == sea.Retention);
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderByDescending(p => p.ID);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex);
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<RepertoryModel, repertory, RepertoryCollection>(lst);
                    result.TotalRecordsCount = n;
                    if (sea != null)
                    {
                        foreach (var model in result)
                        {
                            var d = lst.FirstOrDefault(p => p.ID == model.ID);
                            if (sea.GetProduct)
                                model.Product = Convertor.ConvertTo<ProductModel, product>(d.product);
                            if (sea.GetHouse)
                                model.Warehouse = Convertor.ConvertTo<WarehouseModel, warehouse>(d.warehouse);
                        }
                    }
                }
            }
            return result;
        }
    }
}
